package com.good.shopping.order.service.impl;

import com.good.shopping.order.entity.OrderReturnApplyEntity;
import com.good.shopping.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.good.common.utils.PageUtils;
import com.good.common.utils.Query;

import com.good.shopping.order.dao.OrderItemDao;
import com.good.shopping.order.entity.OrderItemEntity;
import com.good.shopping.order.service.OrderItemService;

// RabbitMQ 监听哪些队列即可  @RabbitListener
@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * queues：声明需要监听的队列
     * channel：当前传输数据的通道
     */
    //@RabbitListener(queues = {"hello-java-queue"})
    // @RabbitHandler: 标在方法上(重载区分不同的消息)
    @RabbitHandler
    public void revieveMessage(Message message, OrderReturnApplyEntity content) {
        //拿到主体内容
        byte[] body = message.getBody();
        //拿到的消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("接受到的消息...内容" + message + "===内容：" + content);
    }


//    @RabbitHandler
//    public void receiverMessage(Message message,OrderReturnApplyEntity content,
//                                Channel channel) throws InterruptedException {
//        //消息体
//        byte[] body = message.getBody();
//        //消息头属性信息
//        MessageProperties properties = message.getMessageProperties();
//        System.out.println("接收到消息...内容:" + content);
////        Thread.sleep(3000);
//        System.out.println("消息处理完成=》" + content.getReturnName());
//        //channel内按顺序自增的
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        System.out.println("deliveryTag:" + deliveryTag);
//        //签收货物，非批量模式
//        try {
//            if (deliveryTag % 2 == 0) {
//                //收货
//                channel.basicAck(deliveryTag, false);
//                System.out.println("签收了货物。。。" + deliveryTag);
//            } else {
//                //退货requeue=false 丢弃  requeue=true发挥服务器，服务器重新入队。
//                channel.basicNack(deliveryTag, false, true);
//                System.out.println("没有签收货物..." + deliveryTag);
//            }
//
//        } catch (Exception e) {
//            //网络中断
//        }
//    }
//

}
